home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 August / CICA - The Ultimate Collection of Shareware for Windows (Disc 2) (August 1995).iso / disc2 / programr / vbasic / generic.exe / GENER.C next >
C/C++ Source or Header  |  1993-04-23  |  8KB  |  262 lines

  1. //---------------------------------------------------------------------------
  2. //        Copyright (C) 1993, Microsoft Corporation
  3. //
  4. // You have a royalty-free right to use, modify, reproduce and distribute
  5. // the Sample Custom Control Files (and/or any modified version) in any way
  6. // you find useful, provided that you agree that Microsoft has no warranty,
  7. // obligation or liability for any Custom Control File.
  8. //---------------------------------------------------------------------------
  9. // gener.c
  10. //---------------------------------------------------------------------------
  11. // Contains control procedure for GENERIC control
  12. //---------------------------------------------------------------------------
  13.  
  14. #include <windows.h>
  15. #include <vbapi.h>
  16. #include "gener.h"
  17. #include "genfun.h"
  18.  
  19. //---------------------------------------------------------------------------
  20. // Global Variables
  21. //---------------------------------------------------------------------------
  22. BOOL bDevTimeInit = FALSE;
  23. HANDLE hModDLL;
  24. HBITMAP HBitmap;
  25. WORD cVBXusers = 0;
  26. USHORT bmWidth;
  27. USHORT bmHeight;
  28.  
  29. /***  Function prototypes  ***/
  30. VOID PASCAL PaintControl(HWND hwnd);
  31.  
  32.  
  33. /****************************************************************************
  34.  *
  35.  *  FUNCTION:  GenericCtlProc(HCTL, HWND, USHORT, USHORT, LONG)
  36.  *
  37.  *  PURPOSE:   This routine is the subclassed window procedure.  Visual Basic
  38.  *             passes VBM_, VBN_, and WM_ messages to this routine.
  39.  *             The custom control determines which messages to process.
  40.  *             Any messages that are not processed need to passed on to the
  41.  *             default message processing routine VBDefControlProc().
  42.  *
  43.  ******************************************************************************/
  44.  
  45. LONG FAR PASCAL _export GenericCtlProc(HCTL hctl, HWND hwnd, USHORT msg,
  46.             USHORT wp, LONG lp)
  47. {
  48.     PGENERIC        pGeneric;
  49.     LPSTR            lpStr;
  50.  
  51.     switch (msg) {
  52.         case WM_SIZE:
  53.             /*
  54.              *    Set the sizing border of the control to the exact size
  55.              *    of the bitmap.
  56.              */
  57.             SetWindowPos(hwnd, NULL, 0, 0, bmWidth, bmHeight,
  58.                 SWP_NOMOVE | SWP_NOZORDER);
  59.             break;
  60.  
  61.         case VBM_CREATED:
  62.             /*
  63.              *    The Generic custom control is an invisible control.  If we
  64.              *    are in design mode, display it; if we are not in design mode,
  65.              *    don't allow the control to be displayed.
  66.              */
  67.             if (VBGetMode() == MODE_RUN)
  68.                 return 0L;
  69.     
  70.             break;
  71.  
  72.         case VBM_SETPROPERTY:
  73.             /*
  74.              *    When the Action property is set to a valid value, execute the
  75.              *    the specified action based on other property values.  The
  76.              *    Action property acts as a pseudo-method.
  77.              */
  78.             pGeneric = (PGENERIC)VBDerefControl(hctl);
  79.         
  80.             switch (wp) {
  81.                 case IPROP_GENERIC_ACTION:
  82.                     switch ((USHORT)lp) {
  83.                         case GEN_STRLEN:
  84.                             lpStr = VBDerefHsz(pGeneric->hszStringValue);
  85.                             pGeneric->usIntegerValue =    GenStrlen(lpStr);
  86.                             break;
  87.                     }
  88.  
  89.                     pGeneric->usAction = (USHORT)lp;
  90.                        return 0L;
  91.             }
  92.             break;
  93.  
  94.         case WM_PAINT:
  95.             /*
  96.              *    The Generic custom control must do it's own painting.
  97.              *    Since the control is never created in run mode, painting
  98.              *    only occurs in design mode.
  99.              */
  100.             PaintControl(hwnd);
  101.             break;
  102.     }
  103.  
  104.     return VBDefControlProc(hctl, hwnd, msg, wp, lp);
  105. }
  106.  
  107.  
  108. /****************************************************************************
  109.  *
  110.  *  FUNCTION:  PaintControl(HWND)
  111.  *
  112.  *  PURPOSE:   This routine handles the painting of the control by creating
  113.  *             a memory image of the bitmap and BitBlt'ing it to the screen.
  114.  *
  115.  ******************************************************************************/
  116.  
  117. VOID PASCAL PaintControl(HWND hwnd)
  118. {
  119.     HDC        hdcMem;
  120.     PAINTSTRUCT ps;
  121.     
  122.     /*
  123.      *    Set up the display space for the bitmap.
  124.      */
  125.     BeginPaint(hwnd, &ps);
  126.     hdcMem = CreateCompatibleDC(ps.hdc);
  127.     if (!hdcMem)
  128.         return;
  129.  
  130.        SelectObject(hdcMem, HBitmap);
  131.  
  132.     /*
  133.      *    Display the bitmap in the sizing rectangle.
  134.      */
  135.     BitBlt(ps.hdc, 0, 0, bmWidth, bmHeight, hdcMem, 0, 0, SRCCOPY);
  136.  
  137.        DeleteDC(hdcMem);
  138.     EndPaint(hwnd, &ps);
  139. }
  140.  
  141.  
  142. /****************************************************************************
  143.  *
  144.  *  FUNCTION:  LibMain(HANDLE, WORD, WORD, LPSTR)
  145.  *
  146.  *  PURPOSE:   Initialize library. This routine is called when the
  147.  *             first client loads the DLL.
  148.  *
  149.  ******************************************************************************/
  150.  
  151. int FAR PASCAL LibMain(HANDLE hModule, WORD wDataSeg, WORD cbHeapSize,
  152.                         LPSTR  lpszCmdLine)
  153. {
  154.     // Avoid warnings on unused (but required) formal parameters
  155.     wDataSeg    = wDataSeg;
  156.     cbHeapSize    = cbHeapSize;
  157.     lpszCmdLine = lpszCmdLine;
  158.  
  159.     hModDLL = hModule;
  160.  
  161.     return 1;
  162. }
  163.  
  164.  
  165. /****************************************************************************
  166.  *
  167.  *  FUNCTION:  VBINITCC(USHORT, BOOL)
  168.  *
  169.  *  PURPOSE:   Register custom control.  This routine is called by Visual Basic
  170.  *             or the Visual Basic emulation layer when the custom control DLL
  171.  *             is loaded for use.
  172.  *
  173.  ******************************************************************************/
  174.  
  175. BOOL FAR PASCAL _export VBINITCC(USHORT usVersion, BOOL fRuntime)
  176. {
  177.     BITMAP bmp;
  178.     
  179.     /*
  180.      *    Count the number of hosts using this VBX.  A host can be VB.EXE,
  181.      *    any .EXE compiled from Visual Basic which uses this custom control,
  182.      *    or any other program which loads and uses VBX files.
  183.      */
  184.     ++cVBXusers;
  185.  
  186.     /*
  187.      *    Load the bitmap resource so that it can be used by WM_PAINT.
  188.      */
  189.     if (!fRuntime && !bDevTimeInit) {
  190.         HBitmap = LoadBitmap(hModDLL, MAKEINTRESOURCE(IDBMP_GENERIC_UP));
  191.         if (HBitmap == (HANDLE)NULL)
  192.             return 0L;
  193.  
  194.         GetObject(HBitmap, sizeof(BITMAP), (LPSTR)&bmp);
  195.         bmWidth = (USHORT)bmp.bmWidth;
  196.         bmHeight = (USHORT)bmp.bmHeight;
  197.  
  198.         // We successfully initialized the stuff we need at dev time
  199.         bDevTimeInit = TRUE;
  200.     }
  201.  
  202.     // Register control(s)
  203.     return VBRegisterModel(hModDLL, &modelGeneric);
  204. }
  205.  
  206. //---------------------------------------------------------------------------
  207. // Unregister custom control.  This routine is called by Visual Basic when
  208. // the custom control DLL (VBX) is being unloaded.
  209. //---------------------------------------------------------------------------
  210. VOID FAR PASCAL _export VBTERMCC(VOID)
  211. {
  212.     --cVBXusers;
  213.  
  214.     if (cVBXusers == 0 && bDevTimeInit) {
  215.         // Free any resources created for Dev environment
  216.         DeleteObject(HBitmap);
  217.     }
  218. }
  219.  
  220. //---------------------------------------------------------------------------
  221. // WEP
  222. //---------------------------------------------------------------------------
  223. // C7 and QCWIN provide default a WEP:
  224. //---------------------------------------------------------------------------
  225. #if (_MSC_VER < 610)
  226.  
  227. int FAR PASCAL WEP(int fSystemExit);
  228.  
  229. //---------------------------------------------------------------------------
  230. // For Windows 3.0 it is recommended that the WEP function reside in a
  231. // FIXED code segment and be exported as RESIDENTNAME.  This is
  232. // accomplished using the alloc_text pragma below and the related EXPORTS
  233. // and SEGMENTS directives in the .DEF file.
  234. //
  235. // Read the comments section documenting the WEP function in the Windows
  236. // 3.1 SDK "Programmers Reference, Volume 2: Functions" before placing
  237. // any additional code in the WEP routine for a Windows 3.0 DLL.
  238. //---------------------------------------------------------------------------
  239. #pragma alloc_text(WEP_TEXT,WEP)
  240.  
  241. //---------------------------------------------------------------------------
  242. // Performs cleanup tasks when the DLL is unloaded.  WEP() is
  243. // called automatically by Windows when the DLL is unloaded (no
  244. // remaining tasks still have the DLL loaded).    It is strongly
  245. // recommended that a DLL have a WEP() function, even if it does
  246. // nothing but returns success (1), as in this example.
  247. //---------------------------------------------------------------------------
  248. int FAR PASCAL WEP
  249. (
  250.     int fSystemExit
  251. )
  252. {
  253.     // Avoid warnings on unused (but required) formal parameters
  254.     fSystemExit = fSystemExit;
  255.  
  256.     return 1;
  257. }
  258. #endif // C6
  259.  
  260. //---------------------------------------------------------------------------
  261.  
  262.